﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Data;
using System.Windows.Forms;
using CooStationsToolsNew.Model;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Media;
using System.Diagnostics;


namespace CooStationsToolsNew
{
    class ProjectTools
    {
       

        public ProjectTools()
        {
        }
        /// <summary>
        /// 清理目录方法
        /// </summary>
        /// <param name="strCurDir"></param>
        public void ClearTempFile(string strCurDir)
        {
            try
            {
                if (Directory.Exists(System.IO.Path.GetDirectoryName(strCurDir)))
                {
                    ///针对当前目录建立目录引用对象
                    DirectoryInfo dirInfo = new DirectoryInfo(strCurDir);

                    ///循环判断当前目录下的文件和目录
                    foreach (FileSystemInfo fsi in dirInfo.GetFileSystemInfos())
                    {
                        ///如果是文件
                        if (fsi is FileInfo)
                        {
                            if (fsi.Name != "cfyconfig.xml" && fsi.Name != "CoolMainWeb.swf" && fsi.Name != "CoolMain.swf" && fsi.Name != "index.htm" && fsi.Name != "MIME.ini")
                            {
                                fsi.Delete();
                            }
                        }
                        ///否则是目录
                        else
                        {
                            Directory.Delete(fsi.FullName, true);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 删除最近使用
        /// </summary>
        public void RemovePastUse(string path)
        {
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load(App.appPath + "config.xml");

            XmlNodeList list = xmlConfig.ChildNodes[1].ChildNodes[1].ChildNodes;

            foreach (XmlNode root in list)
            {
                XmlElement xe = (XmlElement)root;
                if (xe.GetAttribute("proName") == path)
                {
                    if (!File.Exists(xe.GetAttribute("proPath")))
                    {
                        xe.ParentNode.RemoveChild(xe);
                    }
                }
            }
            xmlConfig.Save(App.appPath + "config.xml");

        }
        /// <summary>
        /// 增加到最近使用
        /// </summary>
        public void AddPastUse()
        {
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load(App.appPath + "config.xml");

            XmlNodeList list = xmlConfig.ChildNodes[1].ChildNodes;

            XmlNodeList pastList = GetList("pastuse", list);

            XmlNode root = xmlConfig.ChildNodes[1].ChildNodes[1];

            XmlElement xe = xmlConfig.CreateElement("use");//创建一个<item>节点 

            xe.SetAttribute("proName", App.proName);

            xe.SetAttribute("proPath", App.proPath + "//" + App.proName + ".cfy");

            bool ispast = false;
            for (int i = 0; i < pastList.Count; i++)
            {
                XmlElement listxe = (XmlElement)pastList[i];
                if (App.proName == listxe.GetAttribute("proName"))
                {
                    ispast = true;
                    break;
                }
            }
            if (ispast == false)
            {
                if (pastList.Count >= 9)
                {
                    root.RemoveChild(pastList[8]);
                }
                root.PrependChild(xe);

                xmlConfig.Save(App.appPath + "config.xml");
            }

        }
        /// <summary>
        /// 文件夹复制
        /// </summary>
        /// <param name="sourceDirName">原始路径</param>
        /// <param name="destDirName">目标路径</param>
        /// <returns></returns>
        public void CopyFile(string sourceDirName, string destDirName)
        {
            try
            {
                if (sourceDirName.Substring(sourceDirName.Length - 1) != "\\")
                {
                    sourceDirName = sourceDirName + "\\";
                }
                if (destDirName.Substring(destDirName.Length - 1) != "\\")
                {
                    destDirName = destDirName + "\\";
                }
                if (Directory.Exists(sourceDirName))
                {
                    if (!Directory.Exists(destDirName))
                    {
                        Directory.CreateDirectory(destDirName);
                    }
                    foreach (string item in Directory.GetFiles(sourceDirName))
                    {
                        try
                        {
                            File.Copy(item, destDirName + System.IO.Path.GetFileName(item), true);
                        }
                        catch {  }
                    }
                    foreach (string item in Directory.GetDirectories(sourceDirName))
                    {
                        CopyFile(item, destDirName + item.Substring(item.LastIndexOf("\\") + 1));
                    }
                }
            }
            catch (Exception)
            {
                
            }
        }

        /// <summary>
        /// 清除目录
        /// </summary>
        public void ClearTemp()
        {
            ClearTempFile(App.appPath + "\\temp\\");
        }

        /// <summary>
        /// 打开项目方法
        /// </summary>
        /// <param name="FileUrl"></param>
        public DataTable OpenProject(string FileUrl)
        {
            try
            {
                App.dt.Clear();
                App.eventdt.Clear();
                App.piclist.Clear();
                App.rectlist.Clear();
                App.Pagelist.Clear();
                App.ActionList.Clear();
                App.divlist.Clear();
                App.textlist.Clear();
                App.MiniList.Clear();
            }
            catch
            {
            }
            ClearTemp();
            FileInfo f = new FileInfo(FileUrl);

            DirectoryInfo fi = f.Directory;
            App.proPath = fi.FullName;
            App.proName = fi.Name;

            if (!f.Exists)
            {
                RemovePastUse(App.proName);
                System.Windows.Forms.MessageBox.Show("未找到项目所需文件,请检查文件列表!");
                return null;
            }
            
            GetProMessage(FileUrl);
            FileInfo[] fs = fi.GetFiles();

            bool isHasXml = false;

            for (int i = 0; i < fs.Length; i++)
            {
                if (fs[i].Name == "cfy.xml")
                {
                    isHasXml = true;
                    App.pageName = "cfy";
                    break;
                }
            }
            CopyFile(App.proPath, App.appPath + "\\temp");
            if (isHasXml == true)
            {
                GetPageList(FileUrl);
                GetMini(App.appPath + "\\temp\\Mini");
                App.dt =  OpenCfyXml(App.appPath + "\\temp\\cfy.xml");
                App.eventdt =  OpenCfyeventXml(App.appPath + "\\temp\\cfy_event.xml");
                App.ActionList = OpenActionXml(App.appPath + "\\temp\\cfy_event.xml");
                App.scriptdt = OpenScritpDt(App.appPath + "\\temp\\" + App.pageName + "_event.xml");
                AddPastUse();
                return App.dt;

            }
             
            else
            {
                RemovePastUse(App.proName);
                System.Windows.Forms.MessageBox.Show("未找到项目所需文件,请检查文件列表!");
            }
            return null;
        }

        /// <summary>
        /// 获得缩略图文件
        /// </summary>
        /// <param name="url"></param>
        public void GetMini(string url)
        {
            DirectoryInfo dr = new DirectoryInfo(url);
            if (!dr.Exists)
            {
                return;
            }
            else
            {
                foreach (FileInfo f in dr.GetFiles())
                {
                    if (f.Extension.ToLower() == ".png")
                    {
                       
                        ImageSource img = new BitmapImage(new Uri(f.FullName, UriKind.RelativeOrAbsolute));
                        
                        System.Drawing.Bitmap b = new System.Drawing.Bitmap(f.FullName);
                       // ImageSource ii = b as BitmapImage ;
                        Object[] obj = { img, f.Name.Remove(f.Name.Length - f.Extension.Length) };
                        App.MiniList.Add(obj);
                    }
                }
            }
        }
        /// <summary>
        /// 获得a标签的list
        /// </summary>
        /// <param name="Xmlpath"></param>
        /// <returns></returns>
        public List<object> OpenActionXml(string Xmlpath)
        {
            List<object> actionlist = new List<object>();
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(Xmlpath);
            XmlNodeList list = xdoc.ChildNodes[1].ChildNodes[0].ChildNodes;
            foreach (XmlNode xn in list)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.Name == "item")
                {
                    if (xe.HasChildNodes)
                    {
                        CooStationsToolsNew.Model.Action action = GetAction(xe.ChildNodes);
                       if (action != null)
                       {
                           action.Itemid = xe.GetAttribute("id");
                           actionlist.Add(action);
                       }
                    }
                 
                }
            }
            return actionlist;
        }
        /// <summary>
        /// 得到一个A标签
        /// </summary>
        /// <param name="xmnl"></param>
        /// <returns></returns>
        private CooStationsToolsNew.Model.Action GetAction(XmlNodeList xmnl)
        {
            for (int i = 0; i < xmnl.Count; i++)
            {
                XmlElement xe = (XmlElement)xmnl.Item(i);
                if (xe.Name == "a")
                {
                    CooStationsToolsNew.Model.Action action = new Model.Action();
                    action.Url = xe.GetAttribute("url");
                    action.LoadType = xe.GetAttribute("loadType");
                    string tweenSelfPage = xe.GetAttribute("tweenSelfPage");
                    string[] t = tweenSelfPage.Split('|');
                    SettweenSelfPage(action,t);

                    string tweenTarget = xe.GetAttribute("tweenTargetPage");
                    string[] b = tweenTarget.Split('|');
                    SettweenTarget(b, action);
                    return action;
                }
            }
            return null;
        }
        /// <summary>
        /// 设置SettweenTarget相关属性
        /// </summary>
        /// <param name="t"></param>
        /// <param name="action"></param>
        private void SettweenTarget(string[] t, Model.Action action)
        {
            for (int i = 0; i < t.Length; i++)
            {
                string[] s = t[i].Split(':');
                switch (s[0])
                {
                    case "x":
                        action.X = s[1];
                       // action.IsX = true;
                        break;
                    case "y":
                        action.Y = s[1];
                       // action.IsY = true;
                        break;
                    case "time":
                        action.Time = s[1];
                        //action.IsTimer = true;
                        break;
                    case "alpha":
                        action.Alpha = s[1];
                      //  action.IsAlpha = true;
                        break;
                    case "overwrite":
                        action.Overwrite = s[1];
                        break;
                    case "width":
                        action.Width = s[1];
                      //  action.IsWidth = true;
                        break;
                    case "height":
                        action.Height = s[1];
                      //  action.IsHeight = true;
                        break;
                    case "xscale":
                        action.Xscale = s[1];
                        break;
                    case "yscale":
                        action.Yscale = s[1];
                        break;
                    case "rotation":
                        action.Rotation = s[1];
                      //  action.IsRotation = true;
                        break;
                    case "delay":
                        action.Delay = s[1];
                     //   action.IsDelay = true;
                        break;
                    case "ease":
                        action.Overwrite = s[1];
                        break;
                }
            }
        }
        /// <summary>
        /// 设置tweenSelfPage相关属性
        /// </summary>
        /// <param name="action"></param>
        /// <param name="t"></param>
        private void SettweenSelfPage(CooStationsToolsNew.Model.Action action, string[] t)
        {
            for (int i = 0; i < t.Length; i++)
            {
                string[] s = t[i].Split(':');
                switch (s[0])
                {
                    case "x":
                        action.XSelf = s[1];
                        action.IsXSelf = true;
                        break;
                    case "y":
                        action.YSelf = s[1];
                        action.IsYSelf = true;
                        break;
                    case "time":
                        action.TimeSelf = s[1];
                        action.IsTimerSelf = true;
                        break;
                    case "alpha":
                        action.AlphaSelf = s[1];
                        action.IsAlphaSelf = true;
                        break;
                    case "overwrite":
                        action.OverwriteSelf = s[1];
                        break;
                    case "width":
                        action.WidthSelf = s[1];
                        action.IsWidthSelf = true;
                        break;
                    case "height":
                        action.HeightSelf = s[1];
                        action.IsHeightSelf = true;
                        break;
                    case "xscale":
                        action.XscaleSelf = s[1];
                        break;
                    case "yscale":
                        action.YscaleSelf = s[1];
                        break;
                    case "rotation":
                        action.RotationSelf = s[1];
                        action.IsRotationSelf = true;
                        break;
                    case "delay":
                        action.DelaySelf = s[1];
                        action.IsDelaySelf = true;
                        break;
                    case "ease":
                        action.OverwriteSelf = s[1];
                        break;
                }
            }
        }

        /// <summary>
        /// 得到Page列表
        /// </summary>
        /// <param name="Xmlpath"></param>
        public void GetPageList(string Xmlpath)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(Xmlpath);
            try
            {
                XmlNodeList list = xdoc.ChildNodes[1].ChildNodes[6].ChildNodes;

                foreach (XmlNode xn in list)
                {
                    GetCfyPage(xn.InnerXml);
                }
            }
            catch 
            {
                XmlNode root = xdoc.ChildNodes[1];

                XmlElement xeFape = xdoc.CreateElement("page");
                root.AppendChild(xeFape);

                XmlElement xe = xdoc.CreateElement("name");
                xe.InnerXml = "cfy";
                xeFape.AppendChild(xe);
                xdoc.Save(Xmlpath);
            }
        }


        /// <summary>
        /// 创建项目后生成CFY.XML CFY_EVENT.XML
        /// </summary>
        /// <param name="cfy"></param>
        /// <param name="cfyevent"></param>
        public void CreatCfyXml(string cfy, string cfyevent)
        {
            FileInfo fileinfo = new FileInfo(cfyevent);
            string strname = fileinfo.Name;
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            XmlElement root = doc.CreateElement("cfy");
            root.SetAttribute("event", strname);
            doc.AppendChild(root);
            if (!File.Exists(cfy))
            {
                doc.Save(cfy);
            }
            XmlDocument docevent = new XmlDocument();
            XmlDeclaration decevent = docevent.CreateXmlDeclaration("1.0", "utf-8", null);
            docevent.AppendChild(decevent);
            XmlElement rootevent = docevent.CreateElement("cfyEvent");
            docevent.AppendChild(rootevent);

            XmlElement Events = docevent.CreateElement("Events");
            rootevent.AppendChild(Events);

            XmlElement Scripts = docevent.CreateElement("Scripts");
            rootevent.AppendChild(Scripts);

            XmlElement xe3 = docevent.CreateElement("values");//创建一个<item>节点  
            Scripts.AppendChild(xe3);

            if (!File.Exists(cfyevent))
            {
                docevent.Save(cfyevent);
            }


        }


        /// <summary>
        /// 设置Script
        /// </summary>
        /// <param name="cfyevent"> EVENT地址</param>
        /// <param name="text"></param>
        public void SetScript(string text)
        {
            string cfyevent = App.appPath + "\\temp\\" + App.pageName + "_event.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(cfyevent);
            XmlNode root = doc.ChildNodes[1].ChildNodes[1];
            if (root != null)
            {
                if (root.Name == "Scripts")
                {
                    root.FirstChild.InnerXml = "<![CDATA[" + text + "\r\n]]>";
                }
            }
            else
            {
                XmlNode xe = doc.ChildNodes[1];
                XmlElement xe2 = doc.CreateElement("Scripts");//创建一个<item>节点 
                XmlElement xe3 = doc.CreateElement("values");//创建一个<item>节点 

                xe3.InnerXml = "<![CDATA["+ text + "\r\n]]>";
                xe2.AppendChild(xe3);
                xe.AppendChild(xe2);
            }
            doc.Save(cfyevent);
        }



        /*
        /// <summary>
        /// 创建项目后生成CFY.XML CFY_EVENT.XML
        /// </summary>
        /// <param name="cfy"></param>
        /// <param name="cfyevent"></param>
        public void CreatCfyXml(string cfy, string cfyevent)
        {
            FileInfo fileinfo = new FileInfo(cfyevent);
            string strname = fileinfo.Name;
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            XmlElement root = doc.CreateElement("cfy");
            root.SetAttribute("event", strname);
            doc.AppendChild(root);
            if (!File.Exists(cfy))
            {
                doc.Save(cfy);
            }
            XmlDocument docevent = new XmlDocument();
            XmlDeclaration decevent = docevent.CreateXmlDeclaration("1.0", "utf-8", null);
            docevent.AppendChild(decevent);
            XmlElement rootevent = docevent.CreateElement("cfyEvent");
            docevent.AppendChild(rootevent);

            XmlElement Events = docevent.CreateElement("Events");
            rootevent.AppendChild(Events);

            if (!File.Exists(cfyevent))
            {
                docevent.Save(cfyevent);
            }
        }*/
        /// <summary>
        /// 将PAGE页面加入数组
        /// </summary>
        /// <param name="PageName"></param>
        public void GetCfyPage(string PageName)
        {
            CfyPage cp = new CfyPage();
            cp.Id = "page"+ GetRandom();
            cp.PageName = PageName;
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(App.appPath + "\\temp\\" + PageName + ".xml");
            XmlElement xe = (XmlElement)xdoc.ChildNodes[1];
            if (xe.HasAttribute("width"))
            {
                cp.Width = xe.GetAttribute("width");
            }
            else
            {
                cp.Width = App.proWidth;
            }
            if (xe.HasAttribute("height"))
            {
                cp.Height = xe.GetAttribute("height");
            }
            else
            {
                cp.Height = App.proHeight;
            }
            if (xe.HasAttribute("onloadInit"))
            {
                cp.OnloadInit = xe.GetAttribute("onloadInit");
            }
            App.Pagelist.Add(cp);
        }
        /// <summary>
        /// 打开项目 并将event数据存入数组
        /// </summary>
        /// <param name="Xmlpath"></param>
        /// <returns></returns>
        public DataTable OpenCfyeventXml(string Xmlpath)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ItemID");
            dt.Columns.Add(new DataColumn("object", typeof(Object)));
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(Xmlpath);
            XmlNodeList list = xdoc.ChildNodes[1].ChildNodes[0].ChildNodes;

            foreach (XmlNode xn in list)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.Name == "item")
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = xe.GetAttribute("id");
                    dr[1] = GetClipActionList(xe.ChildNodes);
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
        /// <summary>
        /// 上一次的时间
        /// </summary>
        double lastTime = 0;
        /// <summary>
        /// 上一次的延时
        /// </summary>
        double lastDelay = 0;
        /// <summary>
        /// 上次的ID
        /// </summary>
        string LastId = "";
        /// <summary>
        /// 从XML中得到Action并存入数组
        /// </summary>
        /// <param name="xmnl"></param>
        /// <returns></returns>
        public List<ClipAction> GetClipActionList(XmlNodeList xmnl)
        {
            List<ClipAction> actionlist = new List<ClipAction>();

            for (int i = 0; i < xmnl.Count; i++)
            {
                XmlElement xe = (XmlElement)xmnl.Item(i);

                actionlist.Add(GetClipAction(xe));
            }

            return actionlist;
        }

        /// <summary>
        /// 将XML的元素中存入ClipAction
        /// </summary>
        /// <param name="xe"></param>
        /// <returns></returns>
        public ClipAction GetClipAction(XmlElement xe)
        {
            ClipAction action = new ClipAction();
            action.TargetId = xe.GetAttribute("targetId");
            if (action.TargetId != LastId)
            {
                lastDelay = 0;
                lastTime = 0;
            }
   
            string tweenTarget = xe.GetAttribute("tweenTarget");
            string[] t = tweenTarget.Split('|');
            SetClipAction(t, action);
            return action;
        }
        /// <summary>
        /// 将XML中tweenTarget属性存入到ClipAction
        /// </summary>
        /// <param name="t"></param>
        /// <param name="action"></param>
        public void SetClipAction(string[] t, ClipAction action)
        {
            for (int i = 0; i < t.Length; i++)
            {
                action.Mark = i.ToString();
                string[] s = t[i].Split(':');
                switch (s[0])
                {
                    case "x":
                        action.X = s[1];
                        action.IsX = true;
                        break;
                    case "y":
                        action.Y = s[1];
                        action.IsY = true;
                        break;
                    case "time":
                        action.Time = s[1];

                        action.IsTimer = true;
                        break;
                    case "alpha":
                        action.Alpha = s[1];
                        action.IsAlpha = true;
                        break;
                    case "overwrite":
                        action.Overwrite = s[1];
                        break;
                    case "width":
                        action.Width = s[1];
                        action.IsWidth = true;
                        break;
                    case "height":
                        action.Height = s[1];
                        action.IsHeight = true;
                        break;
                    case "xscale":
                        action.Xscale = s[1];
                        break;
                    case "yscale":
                        action.Yscale = s[1];
                        break;
                    case "rotation":
                        action.Rotation = s[1];
                       // action.IsRotation = true;
                        break;
                    case "delay":

                     
                        double delay = double.Parse(s[1]); //当前行的DELAY
                        delay = double.Parse((delay - lastTime - lastDelay).ToString("F1"));//当前行DELAY - 前面所有的TIME和上一行DELAY
                       
                        if (action.TargetId != LastId)
                        {
                            action.Delay = s[1];
                        }
                        else
                        {
                            action.Delay = delay.ToString(); //赋值
                        }
                            lastDelay = double.Parse(s[1]); //
                        action.IsDelay = true;
                        break;
                    case "ease":
                        action.Overwrite = s[1];
                        break;
                }
            }
          
            lastTime = double.Parse(action.Time);
            LastId = action.TargetId;
        }
        /// <summary>
        /// 打开CFYXML文件并且存入数组
        /// </summary>
        /// <param name="Xmlpath"></param>
        /// <returns></returns>
        public DataTable OpenCfyXml(string Xmlpath)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("type");
            dt.Columns.Add(new DataColumn("object", typeof(Object)));
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(Xmlpath);
            XmlNodeList list = xdoc.ChildNodes[1].ChildNodes;
            AddDivList(list, dt);
            xdoc.Save(Xmlpath);
            return dt;
        }

        /// <summary>
        /// 随即ID
        /// </summary>
        /// <returns></returns>
        public string GetRandom()
        {
            string guid = Guid.NewGuid().ToString();
            int n = guid.IndexOf("-");
            return guid.Substring(0, n);
        }
        /// <summary>
        /// XML中的元素存入数组
        /// </summary>
        /// <param name="xmlNodeList"></param>
        /// <param name="dvdt"></param>
        public void AddDivList(XmlNodeList xmlNodeList, DataTable dvdt)
        {
            foreach (XmlNode xn in xmlNodeList)
            {
                if (xn.Name == "value")
                {
                    return;
                }
                XmlElement xe = (XmlElement)xn;
                if (xe.Name == "div")
                {
                    Div dv = new Div();
                    dv.Id = xe.GetAttribute("id");
                    dv.Name = "d" + GetRandom();
                    xe.SetAttribute("name", dv.Name);
                    dv.X = int.Parse(xe.GetAttribute("x"));
                    dv.Y = int.Parse(xe.GetAttribute("y"));
                    if (xe.HasAttribute("width"))
                    {
                        dv.Width = int.Parse(xe.GetAttribute("width")); 
                    }
                    else
                    {
                        dv.Width = 250;
                    }
                    if (xe.HasAttribute("height"))
                    {
                        dv.Height = int.Parse(xe.GetAttribute("height")); 
                    }
                    else
                    {
                        dv.Height = 250;
                    }
                    if (xe.GetAttribute("alpha") == "")
                    {
                        dv.Alpha = 100;
                    }
                    else
                    {
                        dv.Alpha = int.Parse(xe.GetAttribute("alpha"));
                    }
                    if (xe.HasAttribute("onClick"))
                    {
                        dv.OnClick = xe.GetAttribute("onClick");
                    } if (xe.HasAttribute("onRollOver"))
                    {
                        dv.OnRollOver = xe.GetAttribute("onRollOver");
                    } if (xe.HasAttribute("onRollOut"))
                    {
                        dv.OnRollOut = xe.GetAttribute("onRollOut");
                    }
                    if (xe.HasChildNodes)
                    {
                        AddDivList(xe.ChildNodes, dv.Dt);
                    }

                    DataRow dr = dvdt.NewRow();
                    dr[0] = "div";
                    dr[1] = dv;

                    dvdt.Rows.Add(dr);
                }
                else if (xe.Name == "img")
                {
                    Pic im = new Pic();

  
                    im.Id = xe.GetAttribute("id");
                    im.Name = "p" + GetRandom();
                    xe.SetAttribute("name", im.Name);
                    im.Url = App.appPath + "temp\\" + xe.GetAttribute("url");
                    im.X = int.Parse(xe.GetAttribute("x"));
                    im.Y = int.Parse(xe.GetAttribute("y"));
                    im.Width = int.Parse(xe.GetAttribute("width"));
                    im.Height = int.Parse(xe.GetAttribute("height"));
                    im.Alpha = int.Parse(xe.GetAttribute("alpha"));
                    if (xe.HasAttribute("onClick"))
                    {
                        im.OnClick = xe.GetAttribute("onClick");
                    } if (xe.HasAttribute("onRollOver"))
                    {
                        im.OnRollOver = xe.GetAttribute("onRollOver");
                    } if (xe.HasAttribute("onRollOut"))
                    {
                        im.OnRollOut = xe.GetAttribute("onRollOut");
                    }

                    DataRow dr = dvdt.NewRow();
                    dr[0] = "image";
                    dr[1] = im;
                    dvdt.Rows.Add(dr);
                }
                else if (xe.Name == "text")
                {
                    Text tx = new Text();
                    tx.Id = xe.GetAttribute("id");
                    tx.Name = "t" + GetRandom();
                    xe.SetAttribute("name", tx.Name);
                    tx.Content = xe.FirstChild.InnerText;
                    tx.X = int.Parse(xe.GetAttribute("x"));
                    tx.Y = int.Parse(xe.GetAttribute("y"));
                    tx.Width = int.Parse(xe.GetAttribute("width"));
                    tx.Height = int.Parse(xe.GetAttribute("height"));
                    tx.Align = xe.GetAttribute("align");
                    tx.Html = xe.GetAttribute("html");
                    tx.Bold = xe.GetAttribute("bold");
                    tx.Size = xe.GetAttribute("size");
                    tx.Color = xe.GetAttribute("color");
                    tx.WordWrap = xe.GetAttribute("wordWrap");
                    tx.Multiline = xe.GetAttribute("multiline");


                    DataRow dr = dvdt.NewRow();
                    dr[0] = "text";
                    dr[1] = tx;
                    dvdt.Rows.Add(dr);
                }
                else if (xe.Name == "rect")
                {
                    CooStationsToolsNew.Model.Rect re = new CooStationsToolsNew.Model.Rect();
                    re.Id = xe.GetAttribute("id");
                    re.Name = "r" + GetRandom();
                    xe.SetAttribute("name", re.Name);
                    re.X = int.Parse(xe.GetAttribute("x"));
                    re.Y = int.Parse(xe.GetAttribute("y"));
                    re.Width = int.Parse(xe.GetAttribute("width"));
                    re.Height = int.Parse(xe.GetAttribute("height"));
                    re.LineAlpha = int.Parse(xe.GetAttribute("lineAlpha"));
                    re.LineColor = xe.GetAttribute("lineColor");
                    re.RectAlpha = int.Parse(xe.GetAttribute("rectAlpha"));
                    re.RectColor = xe.GetAttribute("rectColor");
                    re.Border = int.Parse(xe.GetAttribute("border"));
                    if (xe.HasAttribute("onClick"))
                    {
                        re.OnClick = xe.GetAttribute("onClick");
                    } if (xe.HasAttribute("onRollOver"))
                    {
                        re.OnRollOver = xe.GetAttribute("onRollOver");
                    } if (xe.HasAttribute("onRollOut"))
                    {
                        re.OnRollOut = xe.GetAttribute("onRollOut");
                    }

                    DataRow dr = dvdt.NewRow();
                    dr[0] = "rect";
                    dr[1] = re;
                    dvdt.Rows.Add(dr);
                }
            }
        }


        /// <summary>
        /// 得到项目信息
        /// </summary>
        /// <param name="FileUrl"></param>
        public void GetProMessage(string FileUrl)
        {
            XmlDocument xmlPro = new XmlDocument();

            xmlPro.Load(FileUrl);

            App.proType = xmlPro.ChildNodes[1].ChildNodes[2].InnerXml;

            App.proWidth = xmlPro.ChildNodes[1].ChildNodes[3].InnerXml;

            App.proHeight = xmlPro.ChildNodes[1].ChildNodes[4].InnerXml;
        }

        /// <summary>
        /// 查找XmlNodeList
        /// </summary>
        /// <param name="name"></param>
        /// <param name="xl"></param>
        /// <returns></returns>
        public XmlNodeList GetList(string name, XmlNodeList xl)
        {
            foreach (XmlNode xn in xl)
            {
                if (xn.Name == name)
                {
                    return xn.ChildNodes;
                }
                else
                {
                    GetList(name, xn.ChildNodes);
                }
            }
            return null;

        }
        /// <summary>
        /// 保存
        /// </summary>
        public bool Save()
        {
            try
            {
                for (int i = 0; i < App.Pagelist.Count; i++)
                {
                    CfyPage cp = (CfyPage)App.Pagelist[i];
                    SaveDtToXml(App.appPath + "\\temp\\" + cp.PageName + ".xml");
                   // RemoveNameXml(App.appPath + "\\temp\\" + cp.PageName + ".xml");
                }

                if (!Directory.Exists(App.proPath + "\\pic"))
                {
                    Directory.CreateDirectory(App.proPath + "\\pic");
                }
                CopyFile(App.appPath + "\\temp", App.proPath);
                for (int i = 0; i < App.Pagelist.Count; i++)
                {
                    CfyPage cp = (CfyPage)App.Pagelist[i];
                    RemoveNameXml(App.proPath + "\\" + cp.PageName + ".xml");
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 删除XML的元素属性
        /// </summary>
        /// <param name="xmlpath"></param>
        public void RemoveNameXml(string xmlpath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlpath);
            readxml(doc.ChildNodes[1].ChildNodes);
            doc.Save(xmlpath);

        }
        /// <summary>
        /// 递归XML删除所有含有Name属性的节点
        /// </summary>
        /// <param name="xmlnl"></param>
        private void readxml(XmlNodeList xmlnl)
        {
            foreach (XmlNode xl in xmlnl)
            {
                if (xl.Name == "value")
                {
                    return;
                }

                XmlElement xe = (XmlElement)xl;
                if (xl.ChildNodes.Count == 0)
                {
                    xe.RemoveAttribute("name");
                }
                else
                {
                    xe.RemoveAttribute("name");
                    readxml(xl.ChildNodes);
                }
            }
        }
        /// <summary>
        /// 将数组中的数组存入XML
        /// </summary>
        /// <param name="xmlpath"></param>
        public void SaveDtToXml(string xmlpath)
        {
            for (int i = 0; i < App.dt.Rows.Count; i++)
            {
                if (App.dt.Rows[i][0].ToString() == "image")
                {
                    Pic pic = (Pic)App.dt.Rows[i][1];
                    FileInfo f = new FileInfo(pic.Url);
                    pic.Url = "pic/" + f.Name;
                    SaveToXml(pic, xmlpath);
                }
                else if (App.dt.Rows[i][0].ToString() == "rect")
                {
                    CooStationsToolsNew.Model.Rect re = (CooStationsToolsNew.Model.Rect)App.dt.Rows[i][1];
                    SaveToXml(re, xmlpath);
                }
                else if (App.dt.Rows[i][0].ToString() == "div")
                {
                    Div div = (Div)App.dt.Rows[i][1];
                    SaveToXml(div, xmlpath);
                }
                else if (App.dt.Rows[i][0].ToString() == "text")
                {
                    Text text = (Text)App.dt.Rows[i][1];
                    SaveToXml(text, xmlpath);
                }
            }
        }

        /// <summary>
        /// 将这个对象存到XML
        /// </summary>
        /// <param name="ob"></param>
        public void SaveToXml(object ob ,string xmlPath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlPath);
            XmlNodeList xmlnl = doc.ChildNodes[1].ChildNodes;
            if (ob is Pic)
            {
                SaveImgToXml((Pic)ob, xmlnl);
            }
            else if (ob is CooStationsToolsNew.Model.Rect)
            {
                SaveRectToXml((CooStationsToolsNew.Model.Rect)ob, xmlnl);
            }
            else if (ob is Div)
            {
                SaveDivToXml((Div)ob, xmlnl);
            }
            else if (ob is Text)
            {
                SaveTextToXml((Text)ob, xmlnl);
            }
            doc.Save(xmlPath);
        }


        /// <summary>
        /// text存到XML
        /// </summary>
        /// <param name="textBlock"></param>
        /// <param name="xmlnl"></param>
        internal void SaveTextToXml(Text text, XmlNodeList xmlnl)
        {
            foreach (XmlNode xn in xmlnl)
            {
                if (xn.Name == "value")
                {
                    return;
                }
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("name") == text.Name)
                {
                    xe.SetAttribute("x", text.X.ToString());
                    xe.SetAttribute("y", text.Y.ToString());
                    xe.SetAttribute("id", text.Id);
                    xe.SetAttribute("width", text.Width.ToString());
                    xe.SetAttribute("height", text.Height.ToString());

                    if (text.Align != null && text.Align != "")
                    {
                        xe.SetAttribute("align", text.Align);
                    }
                    if (text.Html != null && text.Html != "")
                    {
                        xe.SetAttribute("html", text.Html);
                    }
                    if (text.Bold != null && text.Bold != "")
                    {
                        xe.SetAttribute("bold", text.Bold);
                    }
                    if (text.Size != null && text.Size != "")
                    {
                        xe.SetAttribute("size", text.Size);
                    }
                    if (text.Color != null && text.Color != "")
                    {
                        xe.SetAttribute("color", text.Color);
                    }
                    if (text.WordWrap != null && text.WordWrap != "")
                    {
                        xe.SetAttribute("wordWrap", text.WordWrap);
                    }
                    if (text.Multiline != null && text.Multiline != "")
                    {
                        xe.SetAttribute("multiline", text.Multiline);
                    }
                   
                    if (text.OnClick != null)
                    {
                        xe.SetAttribute("onClick", text.OnClick);
                    }
                    else
                    {
                        if (xe.HasAttribute("onClick"))
                        {
                            xe.RemoveAttribute("onClick");
                        }
                    }
                    if (text.OnRollOut != null)
                    {
                        xe.SetAttribute("onRollOut", text.OnRollOut);
                    }
                    else
                    {
                        if (xe.HasAttribute("onRollOut"))
                        {
                            xe.RemoveAttribute("onRollOut");
                        }
                    }
                    if (text.OnRollOver != null)
                    {
                        xe.SetAttribute("onRollOver", text.OnRollOver);
                    }
                    else
                    {
                        if (xe.HasAttribute("onRollOver"))
                        {
                            xe.RemoveAttribute("onRollOver");
                        }
                    }
                    xe.FirstChild.InnerXml = "<![CDATA[" + text.Content + "]]>";
                    return;
                }
                if (xe.HasChildNodes)
                {
                    SaveTextToXml(text, xe.ChildNodes);
                }
            }
        }
        /// <summary>
        /// DIV存储到XML
        /// </summary>
        /// <param name="div"></param>
        /// <param name="xmlnl"></param>
        public void SaveDivToXml(Div div, XmlNodeList xmlnl)
        {
            foreach (XmlNode xn in xmlnl)
            {
                if (xn.Name == "value")
                {
                    return;
                }
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("name") == div.Name)
                {
                    xe.SetAttribute("x", div.X.ToString());
                    xe.SetAttribute("y", div.Y.ToString());
                    xe.SetAttribute("width", div.Width.ToString());
                    xe.SetAttribute("height",div.Height.ToString());
                    xe.SetAttribute("alpha", div.Alpha.ToString());
                    xe.SetAttribute("id", div.Id);
                    if (div.OnClick != null)
                    {
                        xe.SetAttribute("onClick", div.OnClick);
                    }
                    else
                    {
                        if (xe.HasAttribute("onClick"))
                        {
                            xe.RemoveAttribute("onClick");
                        }
                    }
                    if (div.OnRollOut != null)
                    {
                        xe.SetAttribute("onRollOut", div.OnRollOut);
                    }
                    else
                    {
                        if (xe.HasAttribute("onRollOut"))
                        {
                            xe.RemoveAttribute("onRollOut");
                        }
                    }
                    if (div.OnRollOver != null)
                    {
                        xe.SetAttribute("onRollOver", div.OnRollOver);
                    }
                    else
                    {
                        if (xe.HasAttribute("onRollOver"))
                        {
                            xe.RemoveAttribute("onRollOver");
                        }
                    }
                    return;
                }
                if (xe.HasChildNodes)
                {
                    SaveDivToXml(div, xe.ChildNodes);
                }
            }
        }
        /// <summary>
        /// 将矩形对象存入XML
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="xmlnl"></param>
        public void SaveRectToXml(Model.Rect rect, XmlNodeList xmlnl)
        {
            foreach (XmlNode xn in xmlnl)
            {
                if (xn.Name == "value")
                {
                    return;
                }
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("name") == rect.Name)
                {
                    xe.SetAttribute("x", rect.X.ToString());
                    xe.SetAttribute("y", rect.Y.ToString());
                    xe.SetAttribute("width", rect.Width.ToString());
                    xe.SetAttribute("height", rect.Height.ToString());
                    xe.SetAttribute("rectAlpha", rect.RectAlpha.ToString());
                    xe.SetAttribute("lineAlpha", rect.LineAlpha.ToString());
                    xe.SetAttribute("border", rect.Border.ToString());
                    xe.SetAttribute("lineColor", rect.LineColor);
                    xe.SetAttribute("rectColor", rect.RectColor);
                    xe.SetAttribute("id", rect.Id);
                    if (rect.OnClick != null)
                    {
                        xe.SetAttribute("onClick", rect.OnClick);
                    }
                    else
                    {
                        if (xe.HasAttribute("onClick"))
                        {
                            xe.RemoveAttribute("onClick");
                        }
                    }
                    if (rect.OnRollOut != null)
                    {
                        xe.SetAttribute("onRollOut", rect.OnRollOut);
                    }
                    else
                    {
                        if (xe.HasAttribute("onRollOut"))
                        {
                            xe.RemoveAttribute("onRollOut");
                        }
                    }
                    if (rect.OnRollOver != null)
                    {
                        xe.SetAttribute("onRollOver", rect.OnRollOver);
                    }
                    else
                    {
                        if (xe.HasAttribute("onRollOver"))
                        {
                            xe.RemoveAttribute("onRollOver");
                        }
                    }
                    return;
                }
                if (xe.HasChildNodes)
                {
                    SaveRectToXml(rect, xe.ChildNodes);
                }
            }
        }

        /// <summary>
        /// 保存图片信息到XML
        /// </summary>
        /// <param name="pic"></param>
        /// <param name="xmlnl"></param>
        public void SaveImgToXml(Pic pic, XmlNodeList xmlnl)
        {
            foreach (XmlNode xn in xmlnl)
            {
                if (xn.Name == "value")
                {
                    return;
                }
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("name") == pic.Name)
                {
                    xe.SetAttribute("x", pic.X.ToString());
                    xe.SetAttribute("y", pic.Y.ToString());
                    xe.SetAttribute("width", pic.Width.ToString());
                    xe.SetAttribute("height", pic.Height.ToString());
                    xe.SetAttribute("alpha", pic.Alpha.ToString());
                    xe.SetAttribute("url", pic.Url);
                    xe.SetAttribute("id", pic.Id);
                    if (pic.OnClick != null)
                    {
                        xe.SetAttribute("onClick", pic.OnClick);
                    }
                    else
                    {
                        if (xe.HasAttribute("onClick"))
                        {
                            xe.RemoveAttribute("onClick");
                        }
                    }
                    if (pic.OnRollOut != null)
                    {
                        xe.SetAttribute("onRollOut", pic.OnRollOut);
                    }
                    else
                    {
                        if (xe.HasAttribute("onRollOut"))
                        {
                            xe.RemoveAttribute("onRollOut");
                        }
                    }
                    if (pic.OnRollOver != null)
                    {
                        xe.SetAttribute("onRollOver", pic.OnRollOver);
                    }
                    else
                    {
                        if (xe.HasAttribute("onRollOver"))
                        {
                            xe.RemoveAttribute("onRollOver");
                        }
                    }
                    return;
                }
                if (xe.HasChildNodes)
                {
                    SaveImgToXml(pic, xe.ChildNodes);
                }
            }
        }
       
        /// <summary>
        /// 生成工作区域
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="vb"></param>
        public void InitTheWork(DataTable dt, System.Windows.Controls.Canvas vb)
        {
            ModelTools mt = new ModelTools();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][0].ToString() == "image")
                {
                    CooStationsToolsNew.Model.Pic im = (CooStationsToolsNew.Model.Pic)dt.Rows[i][1];
                   mt.CreateImage(im, vb);
                }
                if (dt.Rows[i][0].ToString() == "text")
                {
                    Text im = (Text)dt.Rows[i][1];
                    mt.CreatText(im, vb);
                }
                if (dt.Rows[i][0].ToString() == "rect")
                {
                    CooStationsToolsNew.Model.Rect im = (CooStationsToolsNew.Model.Rect)dt.Rows[i][1];

                    mt.CreatRect(im, vb);
                }
                if (dt.Rows[i][0].ToString() == "div")
                {
                    Div im = (Div)dt.Rows[i][1];
                    Canvas cv = mt.CreatDiv(im, vb);
                    if (im.Dt != null)
                    {
                        InitTheWork(im.Dt, cv);
                    }
                }
            }
        }

        /// <summary>
        /// 用于粘贴工作区域
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="vb"></param>
        public void InitTheWorkandXml(DataTable dt, System.Windows.Controls.Canvas vb)
        {
            ModelTools mt = new ModelTools();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][0].ToString() == "image")
                {
                    CooStationsToolsNew.Model.Pic im = (CooStationsToolsNew.Model.Pic)dt.Rows[i][1];
                    im.Id = "image" + mt.SetImageID();
                    im.Name = "i" + mt.GetRandom();
                    mt.CreateImage(im, vb);
                    mt.addImgtoxml(im, vb.Name);
                }
                if (dt.Rows[i][0].ToString() == "text")
                {
                    Text im = (Text)dt.Rows[i][1];
                    im.Id = "text" + mt.SetTextID();
                    im.Name = "t" + mt.GetRandom();
                    mt.CreatText(im, vb);
                    mt.addTexttoxml(im, vb.Name);
                }
                if (dt.Rows[i][0].ToString() == "rect")
                {
                    CooStationsToolsNew.Model.Rect im = (CooStationsToolsNew.Model.Rect)dt.Rows[i][1];
                    im.Id = "rect" + mt.SetRectID();
                    im.Name = "r" + mt.GetRandom();
                    mt.CreatRect(im, vb);
                    mt.addRecttoxml(im, vb.Name);
                }
                if (dt.Rows[i][0].ToString() == "div")
                {
                    Div im = (Div)dt.Rows[i][1];
                    im.Id = "div" + mt.SetDivID();
                    im.Name = "d" + mt.GetRandom();
                    Canvas cv = mt.CreatDiv(im, vb);
                    mt.addDivtoxml(im, vb.Name);
                    if (im.Dt != null)
                    {
                        InitTheWorkandXml(im.Dt, cv);
                    }
                }
            }
        }

        public static bool CheckId(string id, DataTable dt)
        {
            bool isId = false;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][0].ToString() == "image")
                {
                    CooStationsToolsNew.Model.Pic im = (CooStationsToolsNew.Model.Pic)dt.Rows[i][1];
                    if (im.Id == id)
                    {
                        isId = true;
                        break;
                    }
                }
                if (dt.Rows[i][0].ToString() == "text")
                {
                    Text im = (Text)dt.Rows[i][1];
                    if (im.Id == id)
                    {
                        isId = true;
                        break;
                    }
                }
                if (dt.Rows[i][0].ToString() == "rect")
                {
                    CooStationsToolsNew.Model.Rect im = (CooStationsToolsNew.Model.Rect)dt.Rows[i][1];
                    if (im.Id == id)
                    {
                        isId = true;
                        break;
                    }
                }
                if (dt.Rows[i][0].ToString() == "div")
                {
                    Div im = (Div)dt.Rows[i][1];
                    if (im.Id == id)
                    {
                        isId = true;
                        break;
                    }
                    else
                    {
                        if (im.Dt != null)
                        {
                            isId = CheckId(id, im.Dt);
                            if (isId == true)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            return isId;
        }

        public static void SetID(string Name, string id, DataTable dt)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][0].ToString() == "image")
                {
                    CooStationsToolsNew.Model.Pic im = (CooStationsToolsNew.Model.Pic)dt.Rows[i][1];
                    if (im.Name == Name)
                    {
                        if (im.OnClick != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onClick";
                            string itemid = id + "_onClick";
                            im.OnClick = itemid;
                            SetItemId(olditemid,itemid , oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;
                        }
                        if (im.OnRollOut != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onRollOut";
                            string itemid = id + "_onRollOut";
                            im.OnClick = itemid;
                            SetItemId(olditemid, itemid, oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;

                        } if (im.OnRollOver != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onRollOver";
                            string itemid = id + "_onRollOver";
                            im.OnClick = itemid;
                            SetItemId(olditemid, itemid, oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;
                        }
                        break;
                    }
                }
                if (dt.Rows[i][0].ToString() == "text")
                {
                    Text im = (Text)dt.Rows[i][1];
                    if (im.Name == Name)
                    {
                        im.Id = id;
                        if (im.OnClick != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onClick";
                            string itemid = id + "_onClick";
                            im.OnClick = itemid;
                            SetItemId(olditemid, itemid, oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;
                        }
                        if (im.OnRollOut != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onRollOut";
                            string itemid = id + "_onRollOut";
                            im.OnClick = itemid;
                            SetItemId(olditemid, itemid, oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;
                        } if (im.OnRollOver != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onRollOver";
                            string itemid = id + "_onRollOver";
                            im.OnClick = itemid;
                            SetItemId(olditemid, itemid, oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;
                        }
                        break;
                    }
                }
                if (dt.Rows[i][0].ToString() == "rect")
                {
                    CooStationsToolsNew.Model.Rect im = (CooStationsToolsNew.Model.Rect)dt.Rows[i][1];
                    if (im.Name == Name)
                    {
                        im.Id = id;
                        if (im.OnClick != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onClick";
                            string itemid = id + "_onClick";
                            im.OnClick = itemid;
                            SetItemId(olditemid, itemid, oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;
                        }
                        if (im.OnRollOut != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onRollOut";
                            string itemid = id + "_onRollOut";
                            im.OnClick = itemid;
                            SetItemId(olditemid, itemid, oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;
                        } if (im.OnRollOver != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onRollOver";
                            string itemid = id + "_onRollOver";
                            im.OnClick = itemid;
                            SetItemId(olditemid, itemid, oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;
                        }
                        break;
                    }
                }
                if (dt.Rows[i][0].ToString() == "div")
                {
                    Div im = (Div)dt.Rows[i][1];
                    if (im.Name == Name)
                    {
                        im.Id = id;
                        if (im.OnClick != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onClick";
                            string itemid = id + "_onClick";
                            im.OnClick = itemid;
                            SetItemId(olditemid, itemid, oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;
                        }
                        if (im.OnRollOut != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onRollOut";
                            string itemid = id + "_onRollOut";
                            im.OnClick = itemid;
                            SetItemId(olditemid, itemid, oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;
                        } if (im.OnRollOver != null)
                        {
                            string oldid = im.Id;
                            string olditemid = oldid + "_onRollOver";
                            string itemid = id + "_onRollOver";
                            im.OnClick = itemid;
                            SetItemId(olditemid, itemid, oldid, id);
                            SetTargetid(olditemid, itemid);
                            im.Id = id;
                        }
                        break;
                    }
                    else
                    {
                        if (im.Dt != null)
                        {
                            SetID(Name,id, im.Dt);
                        }
                    }
                }
            }
         
          
        }

        public static void SetItemId(string OldItemid, string itemid, string oldid, string id )
        {
            for (int i = 0; i < App.eventdt.Rows.Count; i++)
            {
                string Id = App.eventdt.Rows[i][0].ToString();
                if (Id.Equals(OldItemid))
                {
                    App.eventdt.Rows[i][0] = itemid;
                }
                List<ClipAction> actionlist = (List<ClipAction>)App.eventdt.Rows[i][1];
                for (int j = 0; j < actionlist.Count; j++)
                {
                    ClipAction ca = (ClipAction)actionlist[j];
                    if (ca.TargetId.Equals(oldid))
                    {
                        ca.TargetId = id;
                    }
                }
            }

            EventTools et = new EventTools();
            et.SaveEventToXml(App.appPath + "//temp//" + App.pageName + "_event.xml");
        }

        public static void SetTargetid(string oldid, string id)
        {
            for (int i = 0; i < App.ActionList.Count; i++)
            {
                CooStationsToolsNew.Model.Action action = (CooStationsToolsNew.Model.Action)App.ActionList[i];
                if (action.Itemid.Equals(oldid))
                {
                    action.Itemid = id;
                }
            }
            EventTools et = new EventTools();
            et.SaveActionListToXml(App.appPath + "//temp//" + App.pageName + "_event.xml");
        }
       

        /// <summary>
        /// 获得ScriptDt
        /// </summary>
        /// <param name="Xmlpath"></param>
        /// <returns></returns>
        public DataTable OpenScritpDt(string Xmlpath)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ItemID");
            dt.Columns.Add(new DataColumn("object", typeof(Object)));
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(Xmlpath);
            XmlNodeList list = xdoc.ChildNodes[1].ChildNodes[0].ChildNodes;

            foreach (XmlNode xn in list)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.Name == "item")
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = xe.GetAttribute("id");
                    dr[1] = GetScript(xe.ChildNodes);
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

        /// <summary>
        /// 获得script数组列表
        /// </summary>
        /// <param name="itemid"></param>
        /// <returns></returns>
        public List<string> GetScript(XmlNodeList xmnl)
        {
            List<string> a = new List<string>();

            for (int i = 0; i < xmnl.Count; i++)
            {
                XmlElement xe = (XmlElement)xmnl.Item(i);
                if (xe.Name == "scriptFunction")
                {
                     
                    a.Add(xe.GetAttribute("functionName"));
                }

            }
            return a;
        }


        BusyIndicator bid = null;
        public void RunBat(BusyIndicator thisbi)
        {
            //Process pro = new Process();

            //FileInfo file = new FileInfo(batPath);
            //pro.StartInfo.WorkingDirectory = file.Directory.FullName;
            //pro.StartInfo.FileName = batPath;
            //pro.StartInfo.CreateNoWindow = false;
            //pro.Start();
            //pro.WaitForExit();
            //pro.StandardOutput();
          //  File.WriteAllText("test.bat", "@start");
             bid = thisbi;
           // bid.ShowDialog();
             System.Threading.Thread.Sleep(2000);
            ProcessStartInfo psi = new ProcessStartInfo("test.bat");
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            Process p = new Process();
            p=Process.Start(psi);
            p.StartInfo.CreateNoWindow = true;
           // Console.WriteLine("正在执行");
            p.EnableRaisingEvents = true;
           p.StartInfo.UseShellExecute = false;
            p.Exited += new EventHandler(p_Exited);
            p.WaitForExit();
           // Console.WriteLine("执行完毕");
           // Console.ReadKey();

        }

        void p_Exited(object sender, EventArgs e)
        {
          //  System.Threading.Thread.Sleep(2000);
          // Process pro = sender as Process;
         //  pro.Close();
           bid.Close();
          //  StreamReader str = pro.StandardOutput;
            //str=pro.StandardOutput;
        }

    }
}
